package object;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author: admin
 * @Description:
 * @Date: 2019/6/12 16:39
 */

public class User implements Cloneable,Serializable{
    private static final long serialVersionUID = -5809782578272943999L;
    //private String name;
    private String address;
    private User friend;
    private Integer age;

    public User(){
        System.out.println("create User");
    }
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, CloneNotSupportedException, IOException {
        // new
        User user1=new User();
        user1.address="1";
        // 反射
        User user2=(User)Class.forName("object.User").newInstance();
        user2.address="2";
        Constructor constructor=Class.forName("object.User").getConstructor();
        User user3=(User) constructor.newInstance();
        user3.address="3";
        //clone

        user1.friend=user2;

        User user4=(User)user1.clone();
        user4.friend=user3;
        System.out.println(user1.friend.address);
        System.out.println(user4.friend.address);
        System.out.println("==============");
        // Serializable
//        FileOutputStream fileOutputStream=new FileOutputStream("F:/user.dat");
//        ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
//        objectOutputStream.writeObject(user1);
        //System.out.println("==============");
        FileInputStream fileInputStream= new FileInputStream("F:/user.dat");
        ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream);
        User user= (User)objectInputStream.readObject();
        System.out.println(user.address);
    }

}
